<template>
    <div>
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/admin' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item><a href="/admin/brand/list">品牌管理</a></el-breadcrumb-item>
            <el-breadcrumb-item>添加品牌</el-breadcrumb-item>
        </el-breadcrumb>

        <h3 style="margin: 20px 0;">添加品牌</h3>

        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="150px" class="demo-ruleForm">
            <el-form-item label="品牌名称" prop="name">
                <el-input v-model="ruleForm.name"></el-input>
            </el-form-item>
            <el-form-item label="品牌名称的拼音" prop="pinyin">
                <el-input v-model="ruleForm.pinyin"></el-input>
            </el-form-item>
            <el-form-item label="品牌logo的URL" prop="logo">
                <el-input v-model="ruleForm.logo"></el-input>
            </el-form-item>
            <el-form-item label="类别id" prop="categoryId">
                <el-input v-model="ruleForm.categoryId"></el-input>
            </el-form-item>
            <el-form-item label="品牌简介" prop="description">
                <el-input v-model="ruleForm.description"></el-input>
            </el-form-item>
            <el-form-item label="关键词列表" prop="keywords">
                <el-input v-model="ruleForm.keywords"></el-input>
            </el-form-item>
            <el-form-item label="自定义排序序号" prop="sort">
                <el-input v-model="ruleForm.sort"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                ruleForm: {
                    name: '',
                    pinyin: '',
                    logo: '',
                    categoryId: '',
                    description: '',
                    keywords: '',
                    sort: ''
                },
                rules: {
                    name: [
                        {required: true, message: '请输入品牌名称', trigger: 'blur'},
                        {min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur'}
                    ],
                    pinyin: [
                        {required: true, message: '请输入品牌名称的拼音', trigger: 'blur'},
                        {min: 2, max: 25, message: '长度在 2 到 25 个字符', trigger: 'blur'}
                    ],
                    sort: [
                        {pattern: /^[1-9]{1}[0-9]?$/, message: '必须是 0 - 99 之间的数值', trigger: 'blur'}
                    ]
                }
            };
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        let url = 'http://localhost:9080/brands/add-new';
                        console.log('url >>> ' + url);
                        console.log('data >>> ');
                        console.log(this.ruleForm);
                        this.axios
                            .create({headers: {'Authorization': localStorage.getItem('jwt')}})
                            .post(url, this.ruleForm).then((response) => {
                            console.log(response.data);
                            if (response.data.code == 20000) {
                                this.$message({
                                    message: '添加品牌成功！',
                                    type: 'success'
                                });
                            } else {
                                this.$message.error(response.data.message);
                            }
                        }).catch(function(error) {
                            console.log('响应结果为失败！');
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    }
</script>

<style scoped>

</style>